<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>我的消息</title>
    <header th:replace="header::html"></header>
</head>
<body>
<input type="button" class="layui-btn layui-btn-danger layui-btn-xs" value="返回主页" onclick="back()">

<table id="demo" lay-filter="test"></table>


<script type="text/html" id="bar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">删除</a>
</script>

<script>

	$(function ()
	{
		$.ajax({
			type:"POST",
			url:"/api-front/news/updateCount",
			success:function(msg)
			{

			},
			error:function ()
			{
				layer.alert("服务器正忙.....", {icon: 5});
			}
		});
	});
	function back()
	{
		window.location.href="/api-front/getPage?pageName=index";
	}

	layui.use('table', function(){
		var table = layui.table;
		table.render({
			elem: '#demo'
			,type:'POST'
			,url: '/api-front/news/getList'
			,page: true
			,limit:5
			,limits:[5]
			,id:"testReload"
			,cols: [
				[
					{field: 'nid', hide:true}
					,{field: 'ntext', title: '消息内容', width:500}
					,{field: 'ntime', title: '消息时间', width:200}
					,{title:'操作',width:250,toolbar:"#bar"}
				]
			]
		});


		table.on('tool(test)', function(obj)
		{
			var data = obj.data;
			var layEvent = obj.event;
			var tr = obj.tr;
			layer.confirm('确定要删除该条消息吗?', function(index)
			{
				$.ajax({
					type:"POST",
					url:'/api-front/news/deleteNews',
					dataType:"text",
					data:{nid:data.nid},
					success:function(msg)
					{
						if(msg==="yes")
						{
							layer.alert("删除成功", {icon: 1});
							$(".layui-laypage-btn")[0].click();
							layer.close(index);
						}
						else
						{
							layer.alert("删除失败，请重试", {icon: 5});
						}
					},
					error:function ()
					{
						layer.alert("服务器正忙.....", {icon: 5});
					}
				});
			});
		});
	});
</script>
</body>
</html>